var Utils = require('/utils/utils.js');
App({
  data: {
    user: {},
    scene: '',
    showLogin: '',
    labelContent3: {
      title: '美豆值与可爱币说明',
      contentList: [
        '1.美豆值:下次购买美妆品时，可使用减免',
        '2.可爱币：到线下店消费母婴用品时，可使用减免',
        '3.1可爱币=1块钱，1美豆值=1块钱',
        '4.凡标记有返利的产品都可以领取美豆值或可爱币',
        '5.可爱币与美豆值的有效期暂时是永久的有效的',
        '6.更多疑问可在我的页面查看常见问题'
      ]
    },
    showLogin: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),

  },
  onLoad() {
  },
  onLaunch() {
  },


  showLogin(showLogin) {
    var pages = getCurrentPages(); 
    var beforePage = pages[pages.length - 1]; // 当前页面
    if (beforePage) {
      beforePage.setData({
        showLogin: showLogin
      });
    }
  },

  onShow: function (options) {
    this.data.scene = options.scene
    if (options.query && options.query.channel) {
      Utils.CacheService.setChannel(options.query.channel)
    } else {
      Utils.CacheService.setChannel(Utils.__config.merchantId);
    }
    if (options.query && options.query.shareChannel) {
      Utils.CacheService.setShareChannel(options.query.shareChannel)
    }
    let userId = Utils.CacheService.getUserId();
    let shareChannel = options.query.shareChannel;

    if (userId == "" || userId == undefined) {
      if (this.data.canIUse) {
        this.showLogin(true)
      } else {
        this.showLogin(false)
        this.getUserId()
      }
    } else {
      this.showLogin(false)
      //更新渠道信息
      Utils.HttpService.updateUserChannelByShare({ shareChannel: shareChannel }).then(res => {
        console.log(res)
      })
    }
    //需要调用接口，传递用户通过分享进入获取的渠道shareChannel
  },
  getScene() {
    return this.data.scene
  },
  goHome() {
    this.WxService.switchTab('/pages/index/index')
  },

  getUserId() {
    let userId = Utils.CacheService.getUserId();
    if (userId == null || userId == '') {
      Utils.HttpService.getUserInfo();
      return Utils.CacheService.getUserId()
    } else {
      return userId
    }
  },
  globalData: {
    userInfo: Utils.CacheService.getUserId()
  },
  renderImage(path) {
    if (!path) return ''
    if (path.indexOf('http') !== -1) return path
    return `${this.Utils.__config.domain}${path}`;
  },
  getMerchantId() {
    console.log(Utils.__config.merchantId)
    return Utils.__config.merchantId
  },

  showSuccessToast(msg) {
    Utils.WxService.showToast({
      title: msg,
      icon: 'success',
      duration: 1500,
    })
  },
  WxValidate: Utils.WxValidate,
  HttpService: Utils.HttpService,
  HttpServiceNoToast: Utils.HttpServiceNoToast,
  HttpServiceNoCheckLogin: Utils.HttpServiceNoCheckLogin,
  WxService: Utils.WxService,
  CacheService: Utils.CacheService,
  __config: Utils.__config

})